//
//  PCDApiChoosePhoneContact.m
//  PCDMPaasBank
//
//  Created by DK on 2021/3/8.
//  Copyright © 2021 Alibaba. All rights reserved.
//

#import "PCDApiChoosePhoneContact.h"
#import "PCDBankWebViewController.h"

@implementation PCDApiChoosePhoneContact

- (void)handler:(id)data andContext:(PCDContext *)context ResponseCallback:(WVJBResponseCallback )responseCallback
{
      
    
    UIViewController *vc = context.viewController;
    NSString *strCallback = data[@"success"];
    
    if ([vc isKindOfClass:[PCDBankWebViewController class]]) {
        PCDBankWebViewController *temp = (PCDBankWebViewController *)vc;
        temp.isOnShow = NO;
    }
    
    __weak typeof(self) tempSelf = self;
    PCContactRelay relay =^(NSString * name, NSString * phoneNumber, BOOL isCancel){
        //这里将isCancel也回调出来，是为了处理特殊情况，比如重新设置状态栏颜色
                
        if (!isCancel) {

            NSString *strJson = [@{@"name":name,@"mobilePhoneNumber":phoneNumber} JSONRepresentation];
            evaluateJavaScriptOnMainThread(self,strCallback,strJson);
            
        }else{

            NSString *strJson = [@{@"name":@"",@"mobilePhoneNumber":@""} JSONRepresentation];
            evaluateJavaScriptOnMainThread(self,strCallback,strJson);
        }
    };
    if (@available(iOS 9,*)) {
        CNContactPickerViewController *picker = [CNContactPickerViewController pickerWithParentController:vc completion:relay];
        [picker show];
    }else{
        ABPeoplePickerNavigationController *picker =[ABPeoplePickerNavigationController pickerWithParentController:vc completion:relay];
        [picker show];
    }
}

@end
